package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.LevelEvaluation;
import com.jyy.prefabricated.entity.LevelEvaluationCategory;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.LevelEvaluationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class LevelEvaluationService {

    @Autowired
    private LevelEvaluationMapper levelEvaluationMapper;
    
    @Autowired
    private LevelEvaluationCategoryService levelEvaluationCategoryService;

    /**
     * 新增
     * @param levelEvaluation
     * @return
     */
    public int insert(LevelEvaluation levelEvaluation){
        levelEvaluation.setStatus(BaseStatusEnum.INVALID.getValue());
        levelEvaluationMapper.insert(levelEvaluation);
        return levelEvaluation.getId();
    }

    /**
     * 更新
     * @param levelEvaluation
     * @return
     */
    public int update(LevelEvaluation levelEvaluation){
        if(levelEvaluation.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return levelEvaluationMapper.updateByPrimaryKeyWithBLOBs(levelEvaluation);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return levelEvaluationMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return levelEvaluationMapper.batchDelete(dto.getIds());
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return levelEvaluationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public LevelEvaluation selectById(Integer id){
        return levelEvaluationMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @return
     */
    public List<LevelEvaluation> selectByParams(Integer categoryId, String title, Integer status){
        return levelEvaluationMapper.selectByParams(categoryId, title, status);
    }

    /**
     * 根据参数查询
     * @param categoryIds 分类id列表
     * @param title 标题
     * @param status 状态
     * @return
     */
    public List<LevelEvaluation> selectDeepByParams(List<Integer> categoryIds, String title, Integer status){
        return levelEvaluationMapper.selectDeepByParams(categoryIds, title, status);
    }

    /**
     * 查询上一条或下一条信息
     * @param id 当前id
     * @param type 类型
     * @return
     */
    public List<LevelEvaluation> selectByPage(Integer id,String type){
        return levelEvaluationMapper.selectByPage(id, BaseStatusEnum.VALID.getValue(), type);
    }

    /**
     * 根据分类上下级关系查询分类列表
     * @param categoryId
     * @return
     */
    public List<Integer> selectDeepCategoryIds(Integer categoryId){
        List<Integer> categoryIds = new ArrayList<>();
        if(categoryId != null){
            List<LevelEvaluationCategory> list = levelEvaluationCategoryService.selectAll();
            List<LevelEvaluationCategory> categories = levelEvaluationCategoryService.findCategoryIdByPid(categoryId, list);
            categoryIds.addAll(categories.stream().map(e->e.getId()).collect(Collectors.toList()));
            categoryIds.add(categoryId);
        }
        return categoryIds;
    }
}
